
package com.zhu.salesmanagement.views;

import com.dms2.salesmanagement.R;
import com.zhu.salesmanagement.InputMessageActivity;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.models.VisitTaskInfo;
import com.zhu.salesmanagement.provider.SalesSettings;
import com.zhu.salesmanagement.utils.Util;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 重要信息和竞品信息
 */
public class ImportantInfoView extends RelativeLayout implements View.OnClickListener {

    private TextView mNameView = null;
    private TextView mEditText = null;
    private InputMessageActivity mInputMessageActivity = null;
    private VisitTaskInfo mVisitTaskInfo = null;

    public static final int TYPE_ZHONGYAO = 1;
    public static final int TYPE_JINPIN = 2;

    private int mType = TYPE_ZHONGYAO;
    private String mProductype;
    private long mDBId = ProductInfoListView.INVALID_VALUE;

    public ImportantInfoView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ImportantInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    /**
     * 初始化数据
     * @param visitTaskInfo 门店洗洗
     * @param iActivity Activity
     * @param type 竞品OR重要
     * @param ptype 产品类型
     * @param dbid 数据库ID
     */
    public void initData(VisitTaskInfo visitTaskInfo, InputMessageActivity iActivity, int type,
            String ptype, long dbid) {
        mVisitTaskInfo = visitTaskInfo;
        mInputMessageActivity = iActivity;
        mType = type;
        mProductype = ptype;
        mDBId = dbid;
        if(Actions.DEBUG){
            Log.e("zhu",mDBId +"," + mProductype + "," + mType);
        }
        updateName();
    }

    @Override
    public void onFinishInflate() {
        setupViews();
        super.onFinishInflate();
    }

    /**
     * 设置界面
     */
    private void setupViews() {
        mNameView = (TextView) findViewById(R.id.input_name);
        mEditText = (TextView) findViewById(R.id.input_content);

        findViewById(R.id.input_back).setOnClickListener(this);
        findViewById(R.id.input_bottom_back).setOnClickListener(this);
    }

    /**
     * 更新名字数据
     */
    private void updateName() {
        if (mNameView != null) {
            mNameView.setText(mVisitTaskInfo.mName);
        }
        if (mType == TYPE_ZHONGYAO) {
            ((TextView) findViewById(R.id.input_title)).setText(R.string.importantinfo);
        } else if (mType == TYPE_JINPIN) {
            ((TextView) findViewById(R.id.input_title)).setText(R.string.jingpininfo);
        }
        String text = getText();
        if(text != null){
            mEditText.setText(text);
        }
    }

    /**
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (arg0 != null) {
            switch (arg0.getId()) {
                case R.id.input_back:
                case R.id.input_bottom_back:
                    mInputMessageActivity.finish();
                    break;
            }
        }
    }

    /**
     * 获取历史数据
     */
    private String getText(){
        String text = null;
        if(mDBId == ProductInfoListView.INVALID_VALUE){
            return null;
        }
        String query = null;
        if(mType == TYPE_ZHONGYAO){
            query = SalesSettings.StoreCheckInfoColumns.IMPORTINFO;
        }else{
            query = SalesSettings.StoreCheckInfoColumns.JINGPIN;
        }
        Cursor cursor = null;
        cursor = getContext().getContentResolver().query(SalesSettings.StoreCheckInfoColumns.getContentUri(mDBId, false), new String[] { query }, null, null, null);
        if(cursor != null && cursor.moveToFirst()){
            text = cursor.getString(cursor.getColumnIndexOrThrow(query));
        }else{

        }
        if(cursor != null){
        	cursor.close();
        	cursor = null;
        }
        return text;
    }
}
